from functools import wraps

from flask import render_template, request, flash, redirect, url_for, session
from flask_login import login_required, current_user, login_user, logout_user

from app import app,login_manager
from app.model import *
from flask import jsonify, request
from db import db
import uuid
from flask import render_template

@app.route('/insertUser', methods=['POST'])
def insertUser():
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        level = request.form.get('level')
        print(username)
        print(password)
        user = User.query.filter_by(user_name=username).first()
        if user:
            return jsonify("用户名已存在，无法插入")
        else:
            random_uuid = uuid.uuid4()
            new_user = User(id=random_uuid, user_name=username, password=password, role=level)
            print(new_user)
        try:
            db.session.add(new_user)
            db.session.commit()
            flash('Your account has been created! You are now able to log in', 'success')
            return render_template('login.html')
        except:
            flash('Username already exists. Please try a different one.', 'danger')
        return render_template('register.html')


@app.route('/deleteUser', methods=['POST'])
def deleteUser():
    if request.method == 'POST':
        user_id = request.form.get('userId')
        user = User.query.filter_by(id=user_id).first()
        if user:
            db.session.delete(user)
            # 提交事务
            db.session.commit()
            return jsonify("删除成功")
        else:
            return jsonify("删除失败")


@app.route('/updateUser', methods=['POST'])
def updateUser():
    if request.method == 'POST':
        user_id = request.form.get('userId')
        username = request.form.get('username')
        password = request.form.get('password')
        level = request.form.get('level')
        print(username)
        print(password)
        user = User.query.filter_by(id=user_id).first()
        if user:
            user.user_name = username
            user.password = password
            user.role = level
            db.session.commit()
            return jsonify("更新成功")
        else:
            return jsonify("数据有误，更新失败")


@app.route('/selectUser', methods=['GET'])
def selectUser():
    if request.method == 'GET':
        page = request.args.get('page', 1, type=int)
        per_page = request.args.get('per_page', 10, type=int)

        # 进行分页查询
        users = User.query.paginate(page=page, per_page=per_page)

        user_list = [
            {'userId': user.id, 'username': user.user_name, 'password': user.password, 'role': user.role} for
            user in users.items]
        pagination_info = {
            'current_page': users.page,
            'total_pages': users.pages,
            'items_per_page': users.per_page,
            'total_items': users.total
        }
        return {
            'users': user_list,
            'pagination': pagination_info
        }


@app.route('/selectUserById', methods=['GET'])
def selectUserById():
    if request.method == 'GET':
        user_id = request.args.get('userId')
        # 进行分页查询
        user = User.query.filter_by(id=user_id).first()

        return jsonify({
            'user': user.get(),
        })